<template>
	<view class="zxc_centent">
		<view class="cen">
			<view class="body">
				<view class="item">
					<view class="title">登录账号</view>
					<u--input placeholder="请输入账号" v-model="search.username"> </u--input>
				</view>
				<view class="item">
					<view class="title">用户密码</view>
					<u--input placeholder="请输入密码" type="password" v-model="search.password"></u--input>
				</view>
			</view>
			<view class="btn" @click="login"> 登录 </view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				search: {
					username: '',
					password: ''
				},
				xh:null,
			};
		},
		onLoad(e) {
			this.xh = e.xh
			const username = uni.getStorageSync('username')
			const password = uni.getStorageSync('password')
			console.log(!!username,!!password);
			if(!!username&&!!password){
				this.search.username = username
				this.search.password = password
				this.login()
			}
		},
		methods: {
			login() {
				if (this.search.username == '' || this.search.password == '')
					return uni.$u.toast('账号或密码不能为空')
				this.$post(this.$api.login, this.search, res => {
					if (res.code === 200) {
						uni.setStorageSync('username', this.search.username)
						uni.setStorageSync('password', this.search.password)
						uni.$u.toast('登录成功！');
						this.search = {
							username: '',
							password: ''
						}
						uni.setStorageSync('token', res.data.token)
						uni.redirectTo({
							url:'/pages/login/payfees?xh=' + this.xh
						})
					} else {
						uni.$u.toast('登录失败！');
					}
				})
			}
		},
	};
</script>

<style lang="scss" scoped>
	.zxc_centent {
		display: flex;
		align-items: center;
		justify-content: center;
		flex-wrap: wrap;
		padding: 0;
		margin-top: 50rpx;
		height: 100%;

		.cen {
			width: 80%;
			background-color: #fcf9f9;
			border-radius: 30rpx;
			text-align: center;
			padding: 20rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;

			.body {
				margin-top: 30rpx;

				.item {
					.title {
						width: 200rpx;
					}

					display: flex;
					align-items: center;
				}
			}
		}
	}

	.btn {
		width: 80%;
		margin: 50upx 0 0;
		padding: 25upx 0;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #07c160;
		color: #fff;
		border-radius: 100upx;
	}

	.gbc {
		width: 90%;
		margin: 0 auto;
		display: flex;
		align-items: center;
		font-size: 26upx;
		color: #999;
		justify-content: center;
	}
</style>